<template>
  <div>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="22">
          <div class="grid-content bg-purple">
            <span class="subject">学科名称</span>
            <el-input size="small" v-model="params.subjectName"> </el-input>
            <el-button size="mini" @click="clearSearch">清除</el-button>
            <el-button size="mini" type="primary" @click="searchSubject"
              >搜索</el-button
            >
          </div>
        </el-col>
        <el-col :span="2"
          ><div class="grid-content bg-purple">
            <el-button @click="addSubject" size="mini" type="success"
              ><i class="el-icon-edit"></i> 新增学科</el-button
            >
          </div></el-col
        >
      </el-row>
      <p class="count">
        <i class="el-icon-info"></i>
        {{ cTitle }}
      </p>
      <el-table :data="list" style="width: 100%">
        <el-table-column type="index" :index="evenNumber" label="序号">
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template #default="{ row }">
            {{ adddate(row.addDate) }}
          </template>
        </el-table-column>

        <el-table-column prop="isFrontDisplay" label="前台是否显示">
          <template #default="{ row }">
            {{ isDisplay(row.isFrontDisplay) }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="250">
          <template #default="{row}">
            <el-button @click="directorys(row)" type="text" size="small">学科分类</el-button>
            <el-button @click="tags(row)" type="text" size="small">学科标签</el-button>
            <el-button @click="editSubject(row)" type="text" size="small">修改</el-button>
            <el-button @click="delSubject(row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          background
          @current-change="changePage"
          @size-change="changePagesize"
          :page-size="params.pagesize"
          :current-page="params.page"
          :page-sizes="[10, 20, 50, 100]"
          layout="prev, pager,sizes, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <subjects-add ref="formDialog" @closeDialog="closeDialog" :addDialogVisible="addDialogVisible" />
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects.js'
import * as dayjs from 'dayjs'
import subjectsAdd from '../components/subjects-add.vue'
export default {
  name: 'Subjuect',
  components: { subjectsAdd },
  data () {
    return {
      list: [],
      total: 0,
      params: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      search: '',
      addDialogVisible: false
    }
  },
  mounted () {
    this.getList()
  },
  computed: {
    cTitle () {
      return `数据一共${this.total}条`
    }
  },
  methods: {
    async getList () {
      const res = await list(this.params)
      this.list = res.data.items
      this.total = res.data.counts
    },
    evenNumber (index) {
      return index + (this.params.page - 1) * this.params.pagesize + 1
    },
    changePage (page) {
      this.params.page = page
      this.getList()
    },
    changePagesize (pagesize) {
      this.params.pagesize = pagesize
      this.params.page = 1
      this.getList()
    },
    searchSubject () {
      this.params.page = 1
      this.getList()
    },
    clearSearch () {
      this.params.subjectName = ''
      this.params.page = 1
      this.getList()
    },
    adddate (value) {
      return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
    },
    isDisplay (value) {
      const map = {
        0: '否',
        1: '是'
      }
      return map[value]
    },
    addSubject () {
      this.addDialogVisible = true
    },
    closeDialog () {
      this.addDialogVisible = false
      this.getList()
    },
    // 编辑
    editSubject (id) {
      this.addDialogVisible = true
      this.$refs.formDialog.editSubject(id)
    },
    // 删除
    delSubject (id) {
      this.$confirm('确定要删除该学科吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(id)
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    directorys (row) {
      this.$router.push({
        path: '/subjects/directorys',
        query: {
          row
        }
      })
    },
    tags (row) {
      console.log(row)
      this.$router.push({
        path: '/subjects/tags',
        query: {
          row
        }
      })
    }
  }
}
</script>

<style scoped lang='css'>
.page {
  position: relative;
  height: 50px;
}
.el-pagination {
  position: absolute;
  right: 0;
  bottom: 0;
  margin-top: -20px;
}
.grid-content {
  display: flex;
  margin-bottom: 10px;
}
.el-input {
  margin-right: 20px;
  margin-left: 20px;
  width: 200px;
}
.subject {
  margin-top: 5px;
}
.count {
  color: #909399;
  background-color: #f4f4f5;
  padding: 10px;
}
.el-row {
  padding-right: 10px;
}
</style>
